package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/layout"
	"image/color"
	//"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

//
//type ThreeStateButton struct {
//	widget.Button
//	normalImage  *canvas.Image
//	hoverImage   *canvas.Image
//	pressedImage *canvas.Image
//}
//
//func NewThreeStateButton(normal, hover, pressed fyne.Resource) *ThreeStateButton {
//	button := &ThreeStateButton{
//		normalImage:  canvas.NewImageFromResource(normal),
//		hoverImage:   canvas.NewImageFromResource(hover),
//		pressedImage: canvas.NewImageFromResource(pressed),
//	}
//	button.ExtendBaseWidget(button)
//	button.updateImage()
//	return button
//}
//
//func (b *ThreeStateButton) updateImage() {
//	if b.normalImage == nil || b.hoverImage == nil || b.pressedImage == nil {
//		return
//	}
//	b.Icon = b.normalImage.Resource
//}
//
//func (b *ThreeStateButton) MouseIn(*desktop.MouseEvent) {
//	b.Icon = b.hoverImage.Resource
//}
//
//func (b *ThreeStateButton) MouseOut() {
//	b.Icon = b.normalImage.Resource
//}
//
//func (b *ThreeStateButton) MouseDown(*desktop.MouseEvent) {
//	b.Icon = b.pressedImage.Resource
//}
//
//func (b *ThreeStateButton) MouseUp(*desktop.MouseEvent) {
//	b.Icon = b.hoverImage.Resource
//}
//
//func main() {
//	myApp := app.New()
//	myWindow := myApp.NewWindow("Three - StateButtonExample")
//	// 加载三态图片
//	normalImage := theme.ConfirmIcon()
//	hoverImage := theme.InfoIcon()     // 假设悬停时使用 InfoIcon
//	pressedImage := theme.DeleteIcon() // 假设按下时使用 DeleteIcon
//
//	button := NewThreeStateButton(normalImage, hoverImage, pressedImage)
//
//	button.ExtendBaseWidget(button)
//	button.SetOnMouseIn(func(*desktop.MouseEvent) {
//		button.MouseIn(nil)
//	})
//	button.SetOnMouseOut(func() {
//		button.MouseOut()
//	})
//	button.SetOnMouseDown(func(*desktop.MouseEvent) {
//		button.MouseDown(nil)
//	})
//	button.SetOnMouseUp(func(*desktop.MouseEvent) {
//		button.MouseUp(nil)
//	})
//
//	myWindow.SetContent(container.NewVBox(button))
//	myWindow.ShowAndRun()
//}

type ThreeStateButton struct {
	//*widget.Button
	*widget.BaseWidget
	normalImage  *canvas.Image
	hoverImage   *canvas.Image
	pressedImage *canvas.Image
	show         *canvas.Image

	hover   bool
	pressed bool
}

func NewThreeStateButton(normal, hover, pressed fyne.Resource) *ThreeStateButton {
	button := &ThreeStateButton{
		//Button:       widget.NewButton("ASKJHDSJ", func() {}),
		BaseWidget:   &widget.BaseWidget{},
		normalImage:  canvas.NewImageFromResource(normal),
		hoverImage:   canvas.NewImageFromResource(hover),
		pressedImage: canvas.NewImageFromResource(pressed),
	}
	button.ExtendBaseWidget(button)
	button.updateImage()
	return button
}

func (b *ThreeStateButton) MinSize() fyne.Size {
	return fyne.NewSize(100, 50)
	//return b.Button.MinSize()
}
func (a *ThreeStateButton) MouseMoved(ev *desktop.MouseEvent) {
}
func (b *ThreeStateButton) updateImage() {
	if b.pressed {
		b.show = b.pressedImage
		//b.show = canvas.NewImageFromResource(b.pressedImage.Resource) // 确保使用新图
	} else if b.hover {
		b.show = b.hoverImage
		//b.show = canvas.NewImageFromResource(b.hoverImage.Resource)
	} else {
		b.show = b.normalImage
		//b.show = canvas.NewImageFromResource(b.normalImage.Resource)
	}
	b.show.Refresh()
	b.Refresh() // 刷新以更新显示
}

func (b *ThreeStateButton) MouseIn(*desktop.MouseEvent) {
	b.hover = true
	b.updateImage()
}

func (b *ThreeStateButton) MouseOut() {
	b.hover = false
	b.updateImage()
}

func (b *ThreeStateButton) MouseDown(*desktop.MouseEvent) {
	b.pressed = true
	b.updateImage()
}

func (b *ThreeStateButton) MouseUp(*desktop.MouseEvent) {
	b.pressed = false
	b.updateImage()
}

//func (b *ThreeStateButton) CreateRenderer() fyne.WidgetRenderer {
//	return widget.NewSimpleRenderer(b.show)
//}

func (b *ThreeStateButton) Layout(size fyne.Size) {
	b.show.Resize(size) // 确保显示的图像大小正确
}

// CreateRenderer 创建自定义渲染器
func (b *ThreeStateButton) CreateRenderer() fyne.WidgetRenderer {
	return &threeStateButtonRenderer{
		button: b,
		image:  &b.show,
	}
}

// 自定义渲染器结构
type threeStateButtonRenderer struct {
	button *ThreeStateButton
	image  **canvas.Image
}

// Layout 布局
func (r *threeStateButtonRenderer) Layout(size fyne.Size) {
	(*r.image).Resize(size)            // 调整图像大小
	(*r.image).Move(fyne.NewPos(0, 0)) // 移动到容器左上角
}

// MinSize 最小尺寸
func (r *threeStateButtonRenderer) MinSize() fyne.Size {
	return (*r.image).MinSize() // 返回图像的最小大小
}

// Refresh 刷新
func (r *threeStateButtonRenderer) Refresh() {
	(*r.image).Refresh() // 刷新图像
}

// Objects 返回渲染对象
func (r *threeStateButtonRenderer) Objects() []fyne.CanvasObject {
	return []fyne.CanvasObject{(*r.image)}
}

// Destroy 清理
func (r *threeStateButtonRenderer) Destroy() {}

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Three State Button")

	//hoverImage := theme.FolderOpenIcon()
	//pressedImage := theme.FolderIcon()

	//normalImage := theme.ContentRedoIcon()
	//hoverImage := theme.ContentRedoIcon()
	//pressedImage := theme.ContentRedoIcon()

	//normalImage := theme.ContentClearIcon()
	//hoverImage := theme.ContentClearIcon()
	//pressedImage := theme.ContentClearIcon()

	//normalImage := theme.MenuExpandIcon()
	//hoverImage := theme.MenuExpandIcon()
	//pressedImage := theme.MenuExpandIcon()

	normalImage := theme.MenuExpandIcon()
	hoverImage := theme.ContentClearIcon()
	pressedImage := theme.ContentRedoIcon()

	button := NewThreeStateButton(normalImage, hoverImage, pressedImage)

	button.ExtendBaseWidget(button)

	// 设置鼠标事件
	//button.OnMouseIn = button.MouseIn
	//button.OnMouseOut = button.MouseOut
	//button.OnMouseDown = button.MouseDown
	//button.OnMouseUp = button.MouseUp
	button2 := widget.NewButton("1123121221312", func() {

	})
	green := color.NRGBA{R: 0, G: 180, B: 0, A: 255}
	text1 := canvas.NewText("你好", green)
	text3 := canvas.NewText("(右侧)", green)
	content := container.New(layout.NewHBoxLayout(), text1, button2, button, layout.NewSpacer(), text3)

	myWindow.SetContent(container.New(layout.NewVBoxLayout(), content))
	myWindow.Resize(fyne.NewSize(300, 200))
	myWindow.ShowAndRun()
}
func funcName() func() {
	return func() {
		//log.Println("1111")
	}
}
